package org.holynite.fieldmanage.repositories.impl

import org.holynite.fieldmanage.entities.RolePrivilege
import org.holynite.fieldmanage.persistence.RolePrivilegeEAO
import org.holynite.fieldmanage.repositories.BaseEAO

import javax.ejb.Stateless

/**
 * Created by midnite on 15/1/15.
 */
@Stateless(name = "privilegeEAO")
class RolePrivilegesEAOImpl extends BaseEAO implements RolePrivilegeEAO {
	@Override
	List<RolePrivilege> getAllPrivileges() {
		def query = em.createQuery("select rp from RolePrivilege  as rp")
		query.resultList
	}

	@Override
	List<RolePrivilege> getAllPrivilegesInRole(String roleId) {
		def query = em.createQuery("select rp from RolePrivilege as rp where rp.belongsTo=:roleId")
		query.setParameter("roleId", roleId)
		query.resultList
	}

	@Override
	void clearOldPrivileges(String roleId) {
		def query = em.createQuery("delete from RolePrivilege as rp where rp.belongsTo=:roleId")
		query.setParameter("roleId", roleId)
		query.executeUpdate()
	}

	@Override
	void saveAllPrivilegesInRole(List<RolePrivilege> privileges) {
		privileges.each { em.persist(it) }
	}
}
